module Fighter {
	/**
	 * 玩家飞机---刘伟
	 *  
	 * */
	export class PlayerControl {
		constructor(tshowPanel) {
			this.showPanle = tshowPanel;

		}
		/**子弹管理层*/
		public BulletManager: Bullet.BulletManager;
		/**飞机显示在舞台上 */
		public isLiving: boolean;
		/**保存飞机的内容*/
		public planeSprit: egret.Bitmap;

		/**玩家触摸点与飞机距离 */
		private touchDistance: egret.Point = new egret.Point();
		/**开始触摸 */
		private touchStatus: boolean = false;
		/**显示的容器*/
		public showPanle: any;
		//移动的位置
		public moveTarget: egret.Point;
		/**飞机血条*/
		public progress: eui.ProgressBar;
		/**玩家飞机机动画*/
		public copter: egret.MovieClip = new egret.MovieClip();
		/**玩家位置*/
		public PlayPoint: egret.Point;
		/**发射子弹 */
		public fire: egret.Timer;
		public flybool: boolean = false;
		/**飞机初始化 */
		public init(BulletManager) {
			GameUtil.PlayerMessage.instance.isFight = true;
			GameUtil.PlayerMessage.instance.playHp = 4;

			this.BulletManager = BulletManager;
			this.isLiving = true;
			if (this.planeSprit == null) {
				this.planeSprit = new egret.Bitmap();
				this.planeSprit.texture = RES.getRes("playerPlane_png");
			}
			this.PlayPoint = new egret.Point(this.planeSprit.x, this.planeSprit.y);
			this.showPanle.stage.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.mouseDown, this);
			this.showPanle.stage.addEventListener(egret.TouchEvent.TOUCH_END, this.mouseUp, this);
			this.showPanle.addChild(this.planeSprit);
			this.planeSprit.alpha = 0;
			//飞机位置底部居中
			this.planeSprit.x = (this.showPanle.stage.stageWidth - this.planeSprit.width) / 2;
			this.planeSprit.y = (this.showPanle.stage.stageHeight - this.planeSprit.height);
			GameUtil.PlayerMessage.instance.player_position.x = this.planeSprit.x+this.planeSprit.width/2;
			GameUtil.PlayerMessage.instance.player_position.y = this.planeSprit.y+this.planeSprit.height/2;
			this.hpprogress();
			this.progressMove();
			
			if (!this.flybool) {
				this.flybool = true;
				//接受创建的帧动画，以便对动画的位置，visible修改
				this.copter = this.showPanle.Animation.createDeleteAnimation(this.PlayPoint, 4);
				this.AnimationMove();
			}
			this.AnimationMove();
			//this.copter.visible = false;
			if (this.fire == null) {
				this.fire = new egret.Timer(150, 0);
				this.fire.addEventListener(egret.TimerEvent.TIMER, this.createBullet, this);
			}
			this.fire.start();
		}
		/**血条创建*/
		public hpprogress() {
			if (this.progress == null) {
				this.progress = new eui.ProgressBar();
				this.progress.skinName = "resource/assets/FlyM/hpPlane_classics.exml";
				this.progress.width = this.planeSprit.width;

				this.progress.minimum = 0;
			}
			this.progress.maximum = GameUtil.PlayerMessage.instance.playHp;
			this.showPanle.addChild(this.progress);
		}
		/**血条移动*/
		public progressMove() {
			this.progress.x = this.planeSprit.x;
			this.progress.y = this.planeSprit.y - this.planeSprit.height / 2 + 30;
			
		}
		/**帧动画移动*/
		public AnimationMove() {
			this.copter.x = this.planeSprit.x;
			this.copter.y = this.planeSprit.y;
		}
		/**每帧监听 */
		public update() {
			this.hittedByBullet();
			this.PlayDed();
			this.progress.value = GameUtil.PlayerMessage.instance.playHp;
		}
		/**飞机离开舞台 */
		public detrory() {
			if (this.showPanle.stage) {
				this.showPanle.stage.removeEventListener(egret.TouchEvent.TOUCH_BEGIN, this.mouseDown, this);
				this.showPanle.stage.removeEventListener(egret.TouchEvent.TOUCH_END, this.mouseUp, this);
			}
			this.fire.reset();
			if (this.planeSprit.parent) {
				this.planeSprit.parent.removeChild(this.planeSprit);
				this.showPanle.removeChild(this.progress);
			}
			this.isLiving = false;
		}
		/**防止出界*/
		private checkMoveRange() {

			//防止玩家飞机出界
			if (this.planeSprit.x < 0) {
				this.planeSprit.x = 0;
			}
			if (this.planeSprit.y < 0) {
				this.planeSprit.y = 0;
			}
			if (this.planeSprit.x > 640 - this.planeSprit.width) {
				this.planeSprit.x = 640 - this.planeSprit.width;
			}
			if (this.planeSprit.y > 1136 - this.planeSprit.height) {
				this.planeSprit.y = 1136 - this.planeSprit.height;
			}
		}

		//let blt = GameUtil.GamePools.instance.produceBlt("player");
		// blt.startPosition.x = this.planeSprit.x + this.planeSprit.width / 2;
		// blt.startPosition.y = this.planeSprit.y - this.planeSprit.height / 2;
		//AirBattle.Bulletlist.push(blt);
		//需要在游戏逻辑脚本中将子弹添加到舞台


		/**飞机创建子弹*/
		public createBullet() {
			GameUtil.PlayerMessage.instance.isFight = true;
			let point = new egret.Point(this.planeSprit.x + this.planeSprit.width / 2, this.planeSprit.y);
			this.BulletManager.createBullet(0, point)
		}

		/**玩家触摸舞台 */
		public mouseDown(evt: egret.TouchEvent) {
			this.touchStatus = true;
			this.touchDistance.x = evt.stageX - this.planeSprit.x;
			this.touchDistance.y = evt.stageY - this.planeSprit.y;

			if (this.showPanle.stage) {
				this.showPanle.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.mouseMove, this);
			}
		}
		/**玩家拖动舞台 */
		private mouseMove(evt: egret.TouchEvent) {
			this.planeSprit.x = evt.stageX - this.touchDistance.x;
			this.planeSprit.y = evt.stageY - this.touchDistance.y;
		GameUtil.PlayerMessage.instance.player_position.x = this.planeSprit.x+this.planeSprit.width/2;
			GameUtil.PlayerMessage.instance.player_position.y = this.planeSprit.y+this.planeSprit.height/2;
			this.checkMoveRange();
			this.progressMove();
			this.AnimationMove();
		}
		/**玩家触摸结束 */
		public mouseUp(event: egret.Event) {
			this.touchStatus = false;
			if (this.showPanle.stage) {
				this.showPanle.stage.removeEventListener(egret.TouchEvent.TOUCH_MOVE, this.mouseMove, this);
			}
		}
		/**飞机与子弹的碰撞 */
		protected hittedByBullet() {
			if (this.showPanle.BulletManager) {
				let list: any[] = this.showPanle.BulletManager.productBulletus;
				for (let i = 0; i < list.length; i++) {
					let Tbullet: Bullet.BasicBullet = list[i];
					if (list[i].bulletType == 2 && GameHit.GameHit(this.planeSprit, Tbullet.bullet)) {
						GameUtil.PlayerMessage.instance.playHp -= Tbullet.bulletATK;
						Tbullet.bulletHp--;

					}
				}
			}
			if (this.showPanle.flyManager) {
				let list: any[] = this.showPanle.flyManager.productflys;
				for (let i = 0; i < list.length; i++) {
					let TFly: Fighter.BasicFly = list[i];
					if (GameHit.GameHit(this.planeSprit, TFly.fly)) {
						GameUtil.PlayerMessage.instance.playHp -= 2;
						TFly.hp -= 100;
					}
				}
			}
		}
		/**玩家飞机死亡*/
		public PlayDed() {
			if (GameUtil.PlayerMessage.instance.playHp <= 0 && this.planeSprit.parent && this.isLiving) {
				//GameUtil.PlayerMessage.instance.player_position = new egret.Point(320, 1200);
				this.isLiving = false;
				this.flybool=false;
				this.copter.visible = false;
				this.fire.reset();
				this.planeSprit.parent.removeChild(this.planeSprit);
				this.progress.parent.removeChild(this.progress);
			}
		}

	}
}